package com.macllvm.whale.ui.widget;

import android.widget.LinearLayout;
import android.view.LayoutInflater;
import android.content.Context;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.util.Log;
//import com.macllvm.whale.WhaleApplication;
//import com.macllvm.whale.settings.SettingsAccessor;
//import com.macllvm.whale.settings.SystemSettings;
import com.macllvm.whale.inter.SaveCallback;
import com.macllvm.whale.R;

public class SettingEditPanel extends LinearLayout{

    private Context mContext;
    private Button  mBtnOK;
    private Button  mBtnCancel;
    private EditText  mEditText;
    private TextView  mTitle;
    private String    mKey;
    private SaveCallback mCallback;

    public SettingEditPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {
        LayoutInflater inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.edit_panel, this);
        mBtnOK = (Button)findViewById(R.id.btn_ok);
        mBtnOK.setOnClickListener(new  View.OnClickListener() {
            @Override
            public void onClick(View v) {
            String val =  mEditText.getText().toString();
            if(!val.trim().equals("")) {
                mCallback.onSave(mKey, val);
                hide();
            }
            }
        });

        mBtnCancel = (Button)findViewById(R.id.btn_cancel);
        mBtnCancel.setOnClickListener(new  View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
            }
        });
        mEditText =  (EditText)findViewById(R.id.text_editor);
        mTitle  =  (TextView)findViewById(R.id.edit_title);
    }

    public void show(){
        setVisibility(View.VISIBLE);
        Animation showAnim=AnimationUtils.loadAnimation(mContext, R.anim.panel_show);
        this.startAnimation(showAnim);
    }

    public void hide(){
        mEditText.setText("");
        mTitle.setText("");
        Animation hideAnim=AnimationUtils.loadAnimation(mContext, R.anim.panel_hide);
        hideAnim.setAnimationListener(hideAnimListener);
        this.startAnimation(hideAnim);
    }


    private AnimationListener  showAnimListener = new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) { }

        @Override
        public void onAnimationEnd(Animation animation) {
            setVisibility(View.GONE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) { }
    };

    private AnimationListener  hideAnimListener = new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) { }

        @Override
        public void onAnimationEnd(Animation animation) {
            setVisibility(View.GONE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) { }
    };

    public void setEditItemTitle(String val){
        mTitle.setText(val);
    }

    public void setEditItemValue(String val){
        mEditText.setText(val);
    }

    public void setItemKeyValue(String key, Object val, String itemTitle) {
        mKey = key;
        mEditText.setText((String)val);
        mTitle.setText(itemTitle);
    }

    public void addCallback(SaveCallback callback){
        mCallback = callback;
    }

    public void removeCallback(){
        mCallback = null;
    }
}
